package Servlets;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import rmi.*;
import Clases.*;
import java.io.*;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import rmi.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;

/**
 *
 * @author Cédric
 */
public class ProcesaLetra extends HttpServlet {
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
        processRequest(req, res);

        res.setContentType("text/html;charset=UTF-8");
        PrintWriter out = res.getWriter();

        String cancion=req.getParameter("cancion");

        try {
            out.println(
                "<html>" +
                    "<head>" +
                        "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>" +
                        "<title>"+cancion+"</title>" +
                        "<style type=\"text/css\">"+
                            "@import url(style.css);"+
                        "</style>" +
                    "</head>" +
                    "<body>" +
                        "<div id=\"container\">" +
                            "<div id=\"header\">" +
                            "</div>" +
                            "<ul class=\"menu1\">" +
                                "<li><a href=\"./index.jsp\"><b>Inicio</b></a></li>" +
                                "<li><a href=\"ServletYouTube\"><b>Vídeos</b></a></li>" +
                                "<li class=\"current\"><a href=\"Letras\"><b>Letras</b></a></li>" +
                                "<li><a href=\"Imagenes\"><b>Imágenes</b></a></li>" +
                            "</ul>" +
                            "<div id=\"body\"></br></br>" +
                                "<div class=\"box\">" +
                                "<h1 align=center>"+cancion+"</h1>"
            );

                                        ClientRMI almeta = new ClientRMI();
                                        Vector letras=null;
                                        try {
                                            letras=almeta.MetadatosRemoto("192.168.1.101",1085,"Letras",cancion);
                                        } catch (NamingException ex) {
                                            Logger.getLogger(ProcesaLetra.class.getName()).log(Level.SEVERE, null, ex);
                                        }

                                        for(int i=0;i<letras.size();i++){
                                            System.out.println("recibido: "+letras.get(i)+"\n");
                                            out.println(letras.get(i)+"<br/>");
                                        }
                                        out.println("<br/><br/>");

                                out.println("</div><br/><br/>");

            out.println(
                                //"</div></br></br>" +
                            "<br/><br/></div>" +
                        "</div>" +
                    "</body>" +
                "</html>"
            );
        } finally {
            out.close();
        }

    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
